//
//  CustomNavigationController.m
//  iMore
//
//  Created by User on 28/08/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomNavigationController.h"
#import <QuartzCore/QuartzCore.h>

@implementation CustomNavigationController

@synthesize bgImageView;
- (id)initWithRootViewController:(UIViewController *)rootViewController {
	self = [super initWithRootViewController:rootViewController];
    if (self) {
        NSString *imageName =@"top_bg.png";
		UIImage *navigationImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=5.0) {
            [self.navigationBar setBackgroundImage:navigationImage forBarMetrics:UIBarMetricsDefault];
        }else{
            UIImageView *tempImageView = [[UIImageView alloc] initWithImage:navigationImage];
            self.bgImageView =tempImageView;
             [tempImageView release];
             self.bgImageView.layer.zPosition = -100;
            [self.navigationBar insertSubview:self.bgImageView atIndex:0];
             //[self.bgImageView setImage:navigationImage];
        }
        [self reloadNavigationUI];
    }
    return self;
}
//this function to reload navigationUI
- (void)reloadNavigationUI
{
    NSString *imageName =@"top_bg.png";
    UIImage *navigationImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
    [self.bgImageView setImage:navigationImage];
    self.navigationBar.translucent=NO;
    navigationImage =nil;
}


- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:YES];
    
    if ([self.viewControllers count] == 1)
    {
        
    }
    else
    {
        //[self backButton:viewController];
    }
    
}
/*
- (void)backButton:(UIViewController *)viewController{
         NSString *nomalImageName =@"topBar_back.png";
         UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
         [backButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:nomalImageName ofType:nil]] forState:UIControlStateNormal];
         [backButton addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];
         backButton.frame = CGRectMake(0, 0, 58, 29);
         UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
         viewController.navigationItem.leftBarButtonItem = backBarButtonItem;
        [backBarButtonItem release];
       
    }*/

- (void)backToPre {
	[self popViewControllerAnimated:YES];
}
//- (NSArray *) popToRootViewControllerAnimated:(BOOL)animated {
//	return nil;
//}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation==UIDeviceOrientationPortrait);
}
- (BOOL)shouldAutorotate{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



- (void)dealloc{

	[bgImageView release];
	 bgImageView = nil;
    [super dealloc];
}

@end
